package branch.feature.gaode_map.move_track



import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.MapView
import com.amap.api.maps.model.BitmapDescriptor
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.LatLngBounds
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.PolylineOptions
import com.amap.api.maps.utils.overlay.SmoothMoveMarker
import com.example.myapplication.R

// ref link: 
class MainActivity : Activity(), View.OnClickListener {

    private lateinit var mapView: MapView
    private lateinit var aMap: AMap
    private lateinit var mStartButton: Button
    private lateinit var moveMarker: SmoothMoveMarker

    // 点集合
    private val coords = listOf(
        116.3499049793749 to 39.97617053371078,
        116.34978804908442 to 39.97619854213431,
        116.349674596623 to 39.97623045687959,
        116.34955525200917 to 39.97626931100656,
        116.34943728748914 to 39.976285626595036,
        116.34930864705592 to 39.97628129172198,
        116.34918981582413 to 39.976260803938594,
        116.34906721558868 to 39.97623535890678,
        116.34895185151584 to 39.976214717128855,
        116.34886935936889 to 39.976280148755315,
        116.34873954611332 to 39.97628182112874,
        116.34860763527448 to 39.97626038855863,
        116.3484658907622 to 39.976306080391836,
        116.34834585430347 to 39.976358252119745,
        116.34831166130175 to 39.97645709321835,
        116.34827643560175 to 39.97655231226543,
        116.34824186261169 to 39.976658372925556,
        116.34825080406188 to 39.9767570732376,
        116.34825631960626 to 39.976869087779995,
        116.34822111635201 to 39.97698451764595,
        116.34822901510276 to 39.977079745909876,
        116.34822234337618 to 39.97718701787645,
        116.34821627457707 to 39.97730766147824,
        116.34821013897107 to 39.977417746816776,
        116.34820923399242 to 39.97753930933358,
        116.3482045955917 to 39.977652209132174,
        116.3482098441266 to 39.977764016531076,
        116.34820541974412 to 39.97786190186833,
        116.34822159449203 to 39.977958856930286,
        116.3482256370537 to 39.97807288885813,
        116.3482098441266 to 39.978170063673524,
        116.34819564465377 to 39.978266951404066,
        116.34820541974412 to 39.978380693859116,
        116.34819672351216 to 39.97848741209275,
        116.34816588867105 to 39.978593409607825,
        116.34818489339459 to 39.97870216883567,
        116.34818473446943 to 39.978797222300166,
        116.34817728972234 to 39.978893492422685,
        116.34816491505472 to 39.978997133775266,
        116.34815408537773 to 39.97911413849568,
        116.34812908154862 to 39.97920553614499,
        116.34809495907906 to 39.979308267469264,
        116.34805113358091 to 39.97939658036473,
        116.3480310509613 to 39.979491697188685,
        116.3480082124968 to 39.979588529006875,
        116.34799530586834 to 39.979685789111635,
        116.34798818413954 to 39.979801430587926,
        116.3479996420353 to 39.97990758587515,
        116.34799204219203 to 39.980000796262615,
        116.3479912988137 to 39.980116318796085,
        116.34798535084123 to 39.98021407403913,
        116.34797702460183 to 39.980325006125696,
        116.34796288754136 to 39.98042511477518,
        116.34795532232906 to 39.98054129336908,
        116.34797509821901 to 39.980656820423505,
        116.34793922017285 to 39.98074576792626,
        116.34792586413015 to 39.98085620772756,
        116.3478962642899 to 39.98098214824056,
        116.34782449883967 to 39.98108306010269,
        116.34774758827285 to 39.98115277119176,
        116.34761476652932 to 39.98115430642997,
        116.34749135408349 to 39.98114590845294,
        116.34734772765582 to 39.98112245161927,
        116.34708205250223 to 39.98114532232906,
        116.346963237696 to 39.98112245161927,
        116.34681500222743 to 39.981136637759604,
        116.34669622104072 to 39.981146248090866,
        116.34658043260109 to 39.98112495260716,
        116.34643721418927 to 39.9811107163792,
        116.34631638374302 to 39.981085081075676,
        116.34614782996252 to 39.98108046779486,
        116.3460256053666 to 39.981049089345206,
        116.34588814050122 to 39.98104839362087,
        116.34575119741586 to 39.9810544889668,
        116.34562885420186 to 39.981040940565734,
        116.34549232235582 to 39.98105271658809,
        116.34537348820508 to 39.981052294975264,
        116.3453513775533 to 39.980956549928244
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_move_track_gd)

        mapView = findViewById(R.id.map)
        mStartButton = findViewById(R.id.move_start_button)
        mStartButton.setOnClickListener(this)
        mapView.onCreate(savedInstanceState)
        init()
        initMoveMarker()
    }

    private fun init() {
        aMap = mapView.map
    }

    private fun initMoveMarker() {
        addPolylineInPlayGround()
        val points = readLatLngs()

        val bounds = LatLngBounds.builder().apply {
            points.forEach { include(it) }
        }.build()

        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100))

        moveMarker = SmoothMoveMarker(aMap).apply {
            setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.car))
            setPoints(points)
            setTotalDuration(40)
        }

        aMap.setInfoWindowAdapter(infoWindowAdapter)
        moveMarker.setMoveListener { distance ->
            Log.i("MY", "distance: $distance")
            runOnUiThread {
                if (infoWindowLayout != null && title != null && moveMarker.marker.isInfoWindowShown) {
                    title!!.text = "距离终点还有：${distance.toInt()}米"
                }
                if (distance == 0.0) {
                    moveMarker.marker.hideInfoWindow()
                    mMarkerStatus = FINISH_STATUS
                    mStartButton.text = "开始"
                }
            }
        }
        moveMarker.marker.showInfoWindow()
    }

    private fun addPolylineInPlayGround() {
        val list = readLatLngs()
        aMap.addPolyline(
            PolylineOptions().apply {
                customTexture = BitmapDescriptorFactory.fromResource(R.drawable.custtexture)
                addAll(list)
                useGradient(true)
                width(18f)
            }
        )
    }

    private fun readLatLngs(): List<LatLng> {
        return coords.map { (lon, lat) -> LatLng(lat, lon) }
    }

    private val infoWindowAdapter = object : AMap.InfoWindowAdapter {
        override fun getInfoWindow(marker: Marker): View = getInfoWindowView(marker)
        override fun getInfoContents(marker: Marker): View = getInfoWindowView(marker)
    }

    private var infoWindowLayout: LinearLayout? = null
    private var title: TextView? = null
    private var snippet: TextView? = null

    private fun getInfoWindowView(marker: Marker): View {
        if (infoWindowLayout == null) {
            infoWindowLayout = LinearLayout(this).apply {
                orientation = LinearLayout.VERTICAL
                title = TextView(this@MainActivity).apply {
                    setTextColor(Color.BLACK)
                }
                snippet = TextView(this@MainActivity).apply {
                    setTextColor(Color.BLACK)
                }
                addView(title)
                addView(snippet)
                setBackgroundResource(R.drawable.infowindow_bg)
            }
        }
        return infoWindowLayout!!
    }

    private var mMarkerStatus = START_STATUS
    private companion object {
        const val START_STATUS = 0
        const val MOVE_STATUS = 1
        const val PAUSE_STATUS = 2
        const val FINISH_STATUS = 3
    }

    override fun onClick(v: View) {
        when (mMarkerStatus) {
            START_STATUS -> {
                moveMarker.startSmoothMove()
                mMarkerStatus = MOVE_STATUS
                mStartButton.text = "暂停"
            }
            MOVE_STATUS -> {
                moveMarker.stopMove()
                mMarkerStatus = PAUSE_STATUS
                mStartButton.text = "继续"
            }
            PAUSE_STATUS -> {
                moveMarker.startSmoothMove()
                mMarkerStatus = MOVE_STATUS
                mStartButton.text = "暂停"
            }
            FINISH_STATUS -> {
                moveMarker.setPosition(LatLng(39.97617053371078, 116.3499049793749))
                moveMarker.setPoints(readLatLngs())
                moveMarker.marker.showInfoWindow()
                moveMarker.startSmoothMove()
                mMarkerStatus = MOVE_STATUS
                mStartButton.text = "暂停"
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        mapView.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mapView.onSaveInstanceState(outState)
    }

    override fun onDestroy() {
        super.onDestroy()
        mapView.onDestroy()
    }
}